
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);


export const state = () => ({
  token: "",
  currentPath: '/',
  userInfo: {},
  showLogin: false,  // 货币
  currency: '$',
  locale: 'zh',
  brand: [],
  evaluationInfo: JSON.parse(localStorage.getItem('evaluationInfo')) || {},
  currencyType:{
    CNY:{
      label:'人民币',
      value:'￥'
    },
    RUB:{
      label:'卢布',
      value:'₽'
    },
    USD:{
      label:'美元',
      value:'$'
    },
    KZT:{
      label:'坚戈',
      value:'₸'
    },
  }
})

export const mutations = {
  showLogin(state, val) {
    state.showLogin = val;
  },
  SET_LANG(state, locale) {
    state.locale = locale
  },
  SET_TOKEN(state, data) {
    state.token = data.token
  },
  SET_INFO(state, data) {
    state.userInfo = data
    state.showLogin = false
  },
  loginOut(state) {
    state.token = ''
    state.userInfo = {}
  },
  SET_BRAND(state, data) {
    state.brand = data
  },
  isLogin(state, data) {
    state.login = data;
  },
  isTourists(state, data) {
    state.isShowTourists = data;
  },
  SET_LANG(state, locale) {
    state.locale = locale
  },
  cartNum(state, data) {
    state.cartNumber = Number(data);
  },
  /** 店铺全部类型 **/
  SET_MerchantType: (state, merchantType) => {
    state.merchantType = merchantType
  },
  /** 商品分类 **/
  SET_ProductClassify: (state, productClassify) => {
    state.productClassify = productClassify;
  },
  /** 商户全部分类  **/
  SET_MerchantClassify: (state, merchantClassify) => {
    state.merchantClassify = changeNodes(merchantClassify)
  },
  /** 评价列表中的商品信息  **/
  SET_EvaluationInfo: (state, evaluationInfo) => {
    state.evaluationInfo = evaluationInfo
    localStorage.setItem('evaluationInfo', JSON.stringify(evaluationInfo));
  },
  SET_MerPcBanner: (state, merPcBanner) => {
    state.merPcBanner = merPcBanner
    localStorage.setItem('merPcBanner', merPcBanner);
  }
}


export const actions = {
  nuxtServerInit({ commit }, { app }) {
    commit('SET_LANG', app.$cookies.get('locale') ? app.$cookies.get('locale') : 'en');
  },
  /** 店铺类型 **/
  MerTypeList({ state, commit }) {
    return new Promise(reslove => {
      this.$axios
        .get(`/api/front/merchant/all/type/list`).then(res => {
          commit('SET_MerchantType', res.data)
          reslove(res.data);
        });
    }).catch(err => {
      reject(error);
    });
  },
  /** 商户分类 **/
  MerCategoryList({ state, commit }) {
    return new Promise(reslove => {
      this.$axios
        .get(`/api/front/merchant/all/category/list`).then(res => {
          commit('SET_MerchantClassify', res.data)
          reslove(res.data);
        });
    }).catch(err => {
      reject(error);
    });
  },
  /** 商品分类 **/
  getProductClassify({ state, commit }) {
    return new Promise(reslove => {
      this.$axios
        .get(`/api/front/product/category`).then(res => {
          commit('SET_ProductClassify', changeNodes(res.data));
          reslove(res.data);
        });
    }).catch(err => {
      reject(error);
    });
  },
  login({ state, commit }, userInfo) {
    commit('SET_TOKEN', userInfo);
    this.$axios.get(`/api/front/user/info`).then(({ data }) => {
      commit('SET_INFO', { ...data, ...userInfo });
    });
  },
}
/** tree去除 childList=[] 的结构**/
const changeNodes = function (data) {
  if (data.length > 0) {
    for (var i = 0; i < data.length; i++) {
      if (!data[i].childList || data[i].childList.length < 1) {
        data[i].childList = undefined;
      } else {
        changeNodes(data[i].childList);
      }
    }
  }
  return data
};

export const getters = {}
